<?php
//рус
include_once (_CORE_PATH . "module/module_related_base.php");
abstract class decorator_prototype_class extends module_related_base_class
{
    protected $_template = 'default';
    protected $_module;
    protected $_secondActions;
    protected $_ctrls;
    protected $_response;
    protected $_libs;
    
    public function __construct ($module)
    {
        parent::__construct($module);
        $this->_ctrls = $this->_module->GetCtrlCollection();
    }
    
    /**
		обёртка для вызова библиотек
	*/
    protected function Libs()
    {
    	if (!$this->_libs)	$this->_libs = $this->GetModule()->GetLibCollection(); 
    	return $this->_libs;   	
    }     

	protected function Response()
	{
    	if (!$this->_response) $this->_response = $this->GetModule()->GetResponse();
    	return $this->_response;		    
	}      
    
    /**
		запускаем декоратор, т.е. выполняем выплняем установленный метод этого класса
     */
    public function Run ()
    {
        $methodName = $this->_template . 'Tmp';
        $this->CheckTemplate($this->_template);
        $this->$methodName();
        return $this;
    }
    /**
		возвращает текущий шаблон
     */
    public function GetTemplate ()
    {
        return $this->_template;
    }
    /**
		устанавливает шаблон
     */
    public function SetTemplate ($tmpName)
    {
        $this->CheckTemplate($tmpName);
        $this->_template = $tmpName;
        return $this;
    }
    protected function CheckTemplate($tmpName)
    {
        if (! method_exists($this, $tmpName. 'Tmp'))
            throw new Exception('Шаблон ' . $tmpName . ' не найден');        
    }
    public function SetSecondAction ($key, $actionName, $ctrlName, array $params = array())
    {
        if (! $key || ! is_string($key))
            return false;
        if (! $actionName || ! $ctrlName) {
            if (isset($this->_secondActions[$key]))
                unset($this->_secondActions[$key]);
            return $this;
        }
        $this->_secondActions[$key] = array('ctrl' => $ctrlName , 'action' => $actionName, 'params' => $params);
        return $this;
    }
    protected function GetSecondAction ($key)
    {
        if (! isset($this->_secondActions[$key])) return '';
        
        $ctrl = $this->_ctrls->{$this->_secondActions[$key]['ctrl']};
        if (!$ctrl) return '';
        
        return call_user_func_array(array($ctrl, $this->_secondActions[$key]['action']), $this->_secondActions[$key]['params']);
    }
    /**
		вызываем экземпляр шаблонизатора
     */
    protected function GetTemplator ()
    {
        return $this->GetModule()->GetTemplator();
    }
    /**
		Все методы шаблонов должны оканчиваться на Tmp
     */
    protected function DefaultTmp ()
    {}
    
    ########################################################
    
    /**
		шаблон json
     */
    protected function JsonTmp ()
    {
        $this->SetDefaultHeaders();
        $this->Response()->SetHeader('Content-Type', 'application/json; charset=UTF-8');
        $cont = $this->Response()->GetBody();
        $x = new stdClass();
        $x->message = $cont->GetMessage();
        $x->status = $cont->GetStatus();
        $x->data = $cont->GetData();
        $this->Response()->SetBody(json_encode($x));
    }
    
    protected function SimpleJsonTmp ()
    {
        $this->SetDefaultHeaders();
        $this->Response()->SetHeader('Content-Type', 'application/json; charset=UTF-8');
        $cont = $this->Response()->GetBody();
        $this->Response()->SetBody($cont);
    }    
    
    protected function SetDefaultHeaders()
    {	
        $this->Response()->SetHeader('X-Author', 'Nick Zmachynski');
        $this->Response()->SetHeader('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0', true);
        $this->Response()->SetHeader('Pragma', 'no-cache', true);
        $this->Response()->SetHeader('Expires', gmdate('D, d M Y H:i:s', time() - 1000) . ' GMT', true);       
    } 

    /**
		шаблон std
     */
    protected function StdTmp ()
    {
        
    }      
}